<?php
/**
 * 调用第三方数据接口处理类
 * 
 * [WeEngine System] Copyright (c) 2013 WE7.CC
 */
defined('IN_IA') or exit('Access Denied');

class UserapiModuleProcessor extends WeModuleProcessor {
    
    public $name = 'UserapiModuleProcessor';

    public function isNeedInitContext() {
        return 0;
    }
	
   	/**
   	 * 回复可以为微信默信回复XML数据，将直接返回给微信客户端，请保证数据正确性。
   	 * 回复可以为JSON串，由微擎系统构造返回数据格式。
   	 */
    public function respond() {
        global $_W;
    	$rid = $this->rules[0]['id'];
    	$sql = "SELECT * FROM " . tablename('userapi_reply') . " WHERE `rid`=:rid ORDER BY id DESC limit 1";
    	$item = pdo_fetch($sql, array(':rid' => $rid));
    	if (empty($item['id'])) {
    		return array();
    	}
    	if (!strexists($item['apiurl'], '?')) {
    		$item['apiurl'] .= '?';
    	} else {
    		$item['apiurl'] .= '&';
    	}
    	$item['apiurl'] .= http_build_query($this->message, '', '&');
    	$response = http_request($item['apiurl']);
    	
    	if ($response['code'] == '200') {
    		
    	} else {
    		if (!empty($item['default-text'])) {
    			$r = array();
    			$r['FromUserName'] = $this->message['to'];
    			$r['ToUserName'] = $this->message['from'];
    			$r['MsgType'] = 'text';
    			$r['Content'] = $item['default_text'];
    			return $r;
    		}
    		$response = http_request($item['default_apiurl']);
    		if ($response['code'] == '200') {
    			
    		} else {
    			return array();
    		}
    	}
    }

    public function isNeedSaveContext() {
        return false;
    }
    
    private function buildResponse() {
    	
    }
}